//
//  PKPlayInfoViewController.m
//  PianKe
//
//  Created by lanou on 15/12/25.
//  Copyright © 2015年 KFC. All rights reserved.
//

#import "PKPlayInfoViewController.h"
#import "PKMusicPlayer.h"
#import "PKInfoModel.h"
@interface PKPlayInfoViewController ()


// 主题图片
@property (strong, nonatomic) UIImageView *mainImageView;
// 标题
@property (strong, nonatomic) UILabel *titleLabel;
// 喜欢
@property (strong, nonatomic) UILabel *likeLabel;
// 评价
@property (strong, nonatomic) UILabel *commentLabel;
// 下载
@property (strong, nonatomic) UIButton *downLoadButton;
// slider
@property (strong, nonatomic) UISlider *timeSlider;
// 结束时间
@property (strong, nonatomic) UILabel *endTimeLabel;
// 详情info的model
@property (strong, nonatomic) PKInfoModel *infoModel;


@end

@implementation PKPlayInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _mainImageView  = [[UIImageView alloc] initWithFrame:CGRectMake(50, 50, kScreenWidth - 100, kScreenWidth - 100)];
    _mainImageView.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:_mainImageView];
    
    _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, _mainImageView.bottom + 10, kScreenWidth - 40, 30)];
    _titleLabel.backgroundColor = [UIColor greenColor];
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:_titleLabel];
    
    _likeLabel = [[UILabel alloc] initWithFrame:CGRectMake(_mainImageView.left + 20, _titleLabel.bottom + 30, 40, 20)];
    _likeLabel.backgroundColor = [UIColor yellowColor];
    _likeLabel.font = [UIFont systemFontOfSize:13];
    _likeLabel.textColor = [UIColor grayColor];
    [self.view addSubview:_likeLabel];
    
    
    
    UIImageView *likeImageV = [[UIImageView alloc] initWithFrame:CGRectMake(_likeLabel.left - 30, _likeLabel.top, 20, 20)];
    likeImageV.image = [UIImage imageNamed:@"font"];
    [self.view addSubview:likeImageV];
    
    
    _commentLabel = [[UILabel alloc] initWithFrame:CGRectMake(_likeLabel.right + 200, _likeLabel.top, _likeLabel.width, _likeLabel.height)];
    _commentLabel.backgroundColor = [UIColor yellowColor];
    _commentLabel.font = [UIFont systemFontOfSize:13];
    _likeLabel.textColor = [UIColor grayColor];
    [self.view addSubview:_commentLabel];
    
    UIImageView *chatImageV = [[UIImageView alloc] initWithFrame:CGRectMake(_commentLabel.left - 30, _commentLabel.top, 20, 20)];
    chatImageV.image = [UIImage imageNamed:@"chat"];
    [self.view addSubview:chatImageV];
    
    
    
    _downLoadButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    _downLoadButton.frame = CGRectMake(_mainImageView.left, _likeLabel.bottom + 30, 30, 30);
    _downLoadButton.backgroundColor = [UIColor grayColor];
    [_downLoadButton setBackgroundImage:[UIImage imageNamed:@"xiazai2"] forState:(UIControlStateNormal)];
    [self.view addSubview:_downLoadButton];
    
    _timeSlider = [[UISlider alloc] initWithFrame:CGRectMake(_downLoadButton.right + 20, _downLoadButton.top, _mainImageView.width - _downLoadButton.right - 20 , _downLoadButton.height)];
    _timeSlider.backgroundColor = [UIColor greenColor];
    _timeSlider.maximumTrackTintColor = [UIColor grayColor];
    _timeSlider.minimumTrackTintColor = [UIColor blackColor];
    _timeSlider.value = 0;
    [_timeSlider addTarget:self action:@selector(timeChange:) forControlEvents:(UIControlEventValueChanged)];
    [self.view addSubview:_timeSlider];
    
    _endTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(_timeSlider.right + 20, _downLoadButton.top, 50, _downLoadButton.height)];
    _endTimeLabel.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:_endTimeLabel];
    
    _infoModel = [[PKInfoModel alloc] init];

    
    [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(playerPlaying) userInfo:nil repeats:YES];
    
    
     
}




// 处理数据
- (void)setUpData:(PKDetailModel *)model
{
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    parDic[@"auth"] = nil;
    parDic[@"client"] = @"1";
    parDic[@"deviceid"] = @"63A94D37-33F9-40FF-9EBB-481182338873";
    parDic[@"tingid"] = model.tingid;
    parDic[@"version"] = @"3.0.4";
    [PKRequestManager requestWithUrlString:@"http://api2.pianke.me/ting/info" parDic:parDic Method:POST finish:^(NSData *data) {
        
        
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        
          NSDictionary *dataDic = dic[@"data"];
        
        [_infoModel setValuesForKeysWithDictionary:dataDic];
        
          // 再进行赋值,在这里赋值,_infoModle百分之一百已经请求到数据了
        _likeLabel.text = [NSString stringWithFormat:@"%@",_infoModel.like];
        _commentLabel.text = [NSString stringWithFormat:@"%@",_infoModel.comment];

        
    } error:^(NSError *error) {
        
        NSLog(@"%@",error);
        
    }];
    
    
    
}






// 实现slider的方法
- (void)timeChange:(UISlider *)slider
{
    
    [[PKMusicPlayer sharePlayer] seekToTime:slider.value];
    
}



// 实现定时器的方法
- (void)playerPlaying
{
    PKMusicPlayer *player = [PKMusicPlayer sharePlayer];
    
    NSInteger currentTime = player.currentTime;
    NSInteger finishTime = player.finishTime;
    
    NSInteger endTime = finishTime - currentTime;
    
    _endTimeLabel.text = [NSString stringWithFormat:@"%02ld:%02ld",endTime / 60,endTime % 60];
    _timeSlider.maximumValue = finishTime;
    _timeSlider.value = currentTime;
    
    // finishTime 不能为0 ,(=0表示网络不好,返回的也是0)
    if (endTime == 0 && finishTime != 0) {
        
        [player finishPlay];
    }
    
    
    
}

- (void)setViewWithListDetailModel:(PKDetailModel *)model
{
    [_mainImageView sd_setImageWithURL:[NSURL URLWithString:model.imgUrl] placeholderImage:nil];
    _titleLabel.text = model.title;
    
    // 修改like 和 chat
    [self setUpData:model];
    
    // 再进行赋值 , 如果在这里赋值,由于数据请求是一个异步请求的过程,所以此时_infoModel可能还没有请求到数据,所以,赋值,我们放在请求到数据之后:
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
